<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <!--    <link rel="stylesheet" href="../../static/lib/layui-v2.6.3/css/layui.css" media="all">-->
    <link th:href="@{../static/lib/layui-v2.6.3/css/layui.css}" rel="stylesheet">
    <!--    <link rel="stylesheet" href="../../css/public.css" media="all">-->
    <link th:href="@{../static/css/public.css}" rel="stylesheet">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
        .layuimini-main > div {
            display: flex;
            flex-direction: column;
            gap: 20px; /* 组件间距 */
        }

        .header-group {
            display: flex;
            gap: 20px; /* 按钮组间距 */
            align-items: center; /* 垂直居中对齐 */
            width: 100%; /* 占满容器宽度 */
        }

        .layui-btn-container {
            display: flex;
            gap: 10px;
            flex: 1; /* 左侧按钮组自适应 */
            justify-content: flex-start;
        }

        .layui-btn-group {
            display: flex;
            //gap: 10rpx;
            flex: 1; /* 右侧按钮组自适应 */
            justify-content: flex-end;
        }

        .table-wrapper {
            flex: 1; /* 表格自适应剩余空间 */
            overflow: auto; /* 内容超出时滚动 */
        }

        .layui-table {
            width: 100%; /* 表格宽度占满 */
            //min-height: 200rpx; /* 最小高度 */
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn" id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                </div>
            </script>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
            <!-- 操作列 -->
            <script type="text/html" id="auth-state">
                <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
            </script>
        </div>
    </div>
</div>

<script src="../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable','jquery'], function () {
        var $ = layui.$;
        var jquery = jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            toolbar: '#toolbarDemo',
            treeIdName: 'authorityId',
            treePidName: 'parentId',
            elem: '#munu-table',
            url: '/menu/getmenupage3',
            // url:"../static/api/menus.json",
            page: false,
            cols: [
                [
                {type: 'numbers'},
                {field: 'authorityName', minWidth: 200, title: '页面名称'},
                {field: 'authority', title: '权限标识'},
                {field: 'menuUrl', title: '菜单url'},
                {field: 'orderNumber', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        // if (d.isMenu !== 1) {
                        //     return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        // }
                        if (d.parentId === 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });
        table.on('toolbar(munu-table)', function (obj) {
            if (obj.event === 'add') {
                const index = layer.open({
                    title: '添加用户',
                    type: 2,
                    skin: 'layui-layer-admin', // 使用管理台风格
                    shade: 0.3,
                    maxmin: true,
                    area: ['50%', '50%'],
                    content: '../menuAdd.html',
                    success: function(layerElem, index) {
                        // 监听子页面提交事件
                        // const iframeWin = layer.getChildFrame('form', index);
                        // const iframeDoc = layer.getChildFrame('document', index);
                        // iframeWin.submitFormCallback = function(formData) {
                        //     handleAddSubmit(formData, index);
                        // };
                    },
                    end: function() {
                        $(window).off('resize.adjustAddModal'); // 解绑事件
                        table.reload('currentTableId'); // 刷新表格
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });
        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
                if (obj.event === 'edit') {
                    console.log(data);
                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['50%', '50%'],
                        content: '../menuEdit.html?id='+data.authorityId,
                        end: function() {
                            $(window).off('resize.adjustAddModal'); // 解绑事件
                            table.reload('munu-table'); // 刷新表格
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                        $.post('/menu/menuDelete',{id: data.authorityId}, function(res){
                            layer.closeAll();
                            if(res) {  // 直接判断Boolean返回值
                                layer.msg('删除成功 - 用户: ' + data.field.paramName , {icon: 1});
                                // layer.close(index);// 关闭弹出层
                                table.reload('currentTableId'); // 刷新表格
                            } else {
                                layer.msg('添加失败', {icon: 2});
                                // layer.close(index);// 关闭弹出层
                                table.reload('munu-table'); // 刷新表格
                            }
                        }).fail(function(){
                            layer.closeAll();
                            layer.msg('请求失败，请重试', {icon: 2});
                            layer.close(index);// 关闭弹出层
                        });
                        layer.close(index);
                    });
                }
            });
    });

</script>
</body>

</html>